import { deepClone } from "./util";
export default {
  data() {
    return {
      userIdList: [],
      actionAtUser: [],
      serachFilter: "",
      mentionedInfo: {
        type: this.$RongIMLib.MentionedType.SINGAL, //1: @ 所有人 2: @ 指定用户
        userIdList: [], //被 @ 的用户 Id 列表
        mentionedContent: this.$t('chat.At'), //携带扩展信息，例如 `有人@你`
      },
    };
  },
  computed: {
    selectMemberInfo() {
      return this.groupMemberInfo.filter(
        (o) => o.showUserName && o.showUserName.indexOf(this.serachFilter) > -1
      );
    },
  },
  watch: {
    messageContent(nVal, oVal) {
			if(this.isModule != 'true') {
				if (nVal.length > oVal.length) {
				  if (nVal[nVal.length - 1] === "@") {
				    this.serachFilter = "";
				    this.$refs.at.init();
				  } else {
				    var atIndex = nVal.lastIndexOf("@");
				    if (atIndex > -1)
				      this.serachFilter = nVal.slice(atIndex + 1, nVal.length);
				  }
				} else {
				  if (oVal[oVal.length - 1] === "@") {
				    this.$refs.at.close();
				  } else {
				    var atIndex = nVal.lastIndexOf("@");
				    if (atIndex > -1)
				      this.serachFilter = nVal.slice(atIndex + 1, nVal.length);
				    this.delAtUserMixin(nVal, oVal);
				  }
				}
			}
    },
  },
  methods: {
    //删除艾特
    delAtUserMixin(newVal, oldVal) {
      //获取光标位置
      let cursorPosition = 0;
      for (var i = 0; i < oldVal.length; i++) {
        if (newVal[i] !== oldVal[i]) {
          cursorPosition = i;
          break;
        }
      }

      const cursorFirstText = oldVal.slice(0, cursorPosition);
      const atIndex = cursorFirstText.lastIndexOf("@");
      const atToEndstr = cursorFirstText.slice(atIndex, cursorPosition);
      const actionAtUser = deepClone(this.actionAtUser);

      for (var i = 0; i < actionAtUser.length; i++) {
        const text = actionAtUser[i].text;
        if ("@" + text === atToEndstr) {
          const firstText = atIndex - 1 <= 0 ? "" : newVal.slice(0, atIndex);
          const lastText = newVal.slice(atIndex + atToEndstr.length);
          this.messageContent = firstText + lastText;
          this.actionAtUser.splice(i, 1);
          setTimeout(() => {
            this.cursor = atIndex;
          }, 1200);
          return;
        }
      }
    },
    /*@选中*/
    atConfirm(users) {
      if (Array.isArray(users)) {
        console.log(users);
        users.forEach((t, i) => {
          if (t.showUserName && t.userId > 0) {
            if (i == 0) {
              var atIndex = this.messageContent.lastIndexOf("@");
              if (atIndex > -1) {
                  this.messageContent=  this.messageContent.replace("@"+this.serachFilter, "@" + t.showUserName + " ");
              }
              // this.messageContent += t.showUserName + " ";
            } else this.messageContent += "@" + t.showUserName + " ";
            this.mentionedInfo.userIdList.push( String(t.userId));
            this.actionAtUser.push({
              text: t.showUserName,
              id: String(t.userId),
            });
          }
        });
      }
      this.isInputFocus = true
    },
  },
};
